package com.sduept.nwld.dataserver.timer;

import com.esms.MessageData;
import com.sduept.bigdata.fault.common.ParamDesc;
import com.sduept.core.ParamManager;
import com.sduept.core.entity.SystemParameter;
import com.sduept.nwld.dataserver.manager.message.MosMessagePushManager;
import com.sduept.nwld.dataserver.manager.rota.RotaPersonManager;
import com.sduept.nwld.dataserver.model.rota.RotaPerson;
import com.sduept.nwld.mobile.manager.MessagePushManager;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.manager.PersonnelManager;
import com.sduept.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 每周早9点定时推送第二天值班消息
 * 
 * @author leodexin
 *
 */
@Service
@Slf4j
public class RotaTimerSchedule {
	@Autowired
	private RotaPersonManager rpManager;
	@Autowired
	private MessagePushManager mpManager;
	@Autowired
	private MosMessagePushManager mosMessagePushManager;
	@Autowired
	private PersonnelManager personnelManager;
	@Autowired
	private ParamManager pc;

//	@Schedule(hour = "9", minute = "0", second = "0",persistent=false)
	@Scheduled(cron = "0 0 9 * * ?")
	public void rotaScheduleTimer() {
		Calendar cal1 = Calendar.getInstance();
		cal1.get(Calendar.DATE);
		cal1.add(Calendar.DATE, 1);
		Date rotaDate = DateUtil.getDateByString(DateUtil.getStringDate(cal1.getTime(), DateUtil.pattern_ymd), DateUtil.pattern_ymd);
		List<RotaPerson> rps = rpManager.findRotaPersonByRoteDate(rotaDate);
		try {
			smsPush(rps);
		} catch (Exception e) {
			log.error("短信消息群发异常！");
			e.printStackTrace();
		}
		try {
			if (rps != null && !rps.isEmpty()) {
				for (RotaPerson rp : rps) {
					mpManager.rotaPersonPush(rp);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 短信消息推送(不管谁值班都要推送给郑茂然和陈朝晖,在系统参数中配置)
	 * 
	 * @param rps
	 * @throws Exception
	 */
	private void smsPush(List<RotaPerson> rps) throws Exception {
		SystemParameter systemParameter = pc.get(ParamDesc.ROTA_MESSAGE_PUSH_RECEIVER);
		String[] personCodes = null;
		if(null!=systemParameter) {
			String personStr=pc.get(ParamDesc.ROTA_MESSAGE_PUSH_RECEIVER).getValue();
			if(StringUtils.isNotEmpty(personStr)) {
				personCodes=personStr.split(",");
			}
		}
		List<PersonnelEntityBean> persons=personnelManager.findPersonnelByCodes(personCodes);//除值班人以外的信息接收者
		List<MessageData> msgList = new ArrayList<MessageData>();
		for (RotaPerson person : rps) {
			if (StringUtils.isEmpty(person.getPersonId())) {
				continue;
			}
			String rotaDate=DateUtil.getStringDate(person.getRotaDate(), DateUtil.pattern_ymd);
			String content=getMsgConten(person.getPersonName(), rotaDate);
			PersonnelEntityBean personnelEntityBean = personnelManager.findPersonnelByCode(person.getPersonId());
			if (null == personnelEntityBean) {
				for (PersonnelEntityBean p : persons) {
					if (StringUtils.isNotEmpty(p.getTelephone())) {
						msgList.add(new MessageData(p.getTelephone(), content));
					}
				}
			} else {
				if (StringUtils.isNotEmpty(personnelEntityBean.getTelephone())) {
					msgList.add(new MessageData(personnelEntityBean.getTelephone(), content));
				}
				for (PersonnelEntityBean p : persons) {
					if (!p.getCode().equals(personnelEntityBean.getCode())) {
						if (StringUtils.isNotEmpty(p.getTelephone())) {
							msgList.add(new MessageData(p.getTelephone(), content));
						}
					}
				}
				
			}
			
		}
		mosMessagePushManager.sendMsgByGroup(msgList, "值班信息");
	}
	
	
	/**
	 * 根据值班人员和值班日期生成短信消息内容
	 * @param name
	 * @param date
	 * @return
	 */
	private String getMsgConten(String name,String date){
		String result="";
		SystemParameter systemParameter = pc.get(ParamDesc.MOS_MSG_CONTENT_TEMPLATE);
		if(null==systemParameter||StringUtils.isEmpty(systemParameter.getValue())){
			result="#{name}，本周末/节假日（#{date}）轮到您值班，请做好值班准备，如有问题，及时协调其他人值班。";
		}else{
			result = systemParameter.getValue();
		}
		result=result.replace("#{name}", name);
		result=result.replace("#{date}", date);
		return result;
	}

}
